package com.cloud.lan.entity.base.common;

import java.io.Serializable;

import lombok.Data;

/**
 * 分页
 * @author ErKang
 * @date Created in 2021/12/5 04:45
 * @title Page.java
 * Department: Product development
 */
@Data
public class Page implements Serializable {

    private static final long serialVersionUID = 4715241161670361683L;

    /** 当前页 */
    protected int currentPage = 1;

    /** 每页记录数，默认10条 */
    protected int pageSize = 10;

    /** 记录总数 */
    protected long count;

    /** 页数 */
    protected int pages;

    /** 当前页的索引 */
    protected int index;

    public Page() {
        this.currentPage = 1;
        this.pageSize = 10;
    }

    public Page(int currentPage, int pageSize) {
        this.currentPage = currentPage;
        this.pageSize = pageSize;
    }

    /**
     * 获取当前页
     *
     * @return 当前页
     */
    public int getCurrentPage() {
        return currentPage;
    }

    /**
     * 设置当前页
     *
     * @param currentPage 当前页
     */
    public void setCurrentPage(int currentPage) {
        this.currentPage = currentPage;
    }

    /**
     * 获取每页记录数
     *
     * @return 每页记录数
     */
    public int getPageSize() {
        return pageSize;
    }

    /**
     * 设置每页记录数
     *
     * @param pageSize 每页记录数
     */
    public void setPageSize(int pageSize) {
        this.pageSize = pageSize;
    }

    /**
     * 获取记录总数
     *
     * @return 记录总数
     */
    public long getCount() {
        return count;
    }

    /**
     * 设置记录总数
     *
     * @param count 记录总数
     */
    public void setCount(long count) {
        this.count = count;

        if (count > 0) {
            // 计算页数
            this.pages = (int) (this.count / this.pageSize);

            if (this.count % this.pageSize > 0) {
                this.pages++;
            }

            // 调整当前页
            if (this.currentPage > this.pages) {
                this.currentPage = this.pages;
            }

            // 计算当前页的索引
            this.index = (this.currentPage - 1) * this.pageSize;
        }
    }

    public void init() {
        if (this.currentPage <= 1) {
            this.currentPage = 1;
        }
        if (this.pageSize < 0) {
            pageSize = 10;
        }
    }

    /**
     * 获取页数
     *
     * @return 页数
     */
    public int getPages() {
        return pages;
    }

    /**
     * 获取当前页的索引
     *
     * @return 当前页的索引
     */
    public int getIndex() {
        return index;
    }

    /* (non-Javadoc)
     * @see java.lang.Object#toString()
     */
    @Override
    public String toString() {
        return "Page [currentPage="
                + currentPage
                + ", pageSize="
                + pageSize
                + ", count="
                + count
                + ", pages="
                + pages
                + ", index="
                + index
                + "]";
    }
}
